package com.ap.core.email.impl;


import com.ap.core.email.api.EmailPayloadInfo;
import com.ap.core.email.api.EmailService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.mail.Session;

/**
 * Implementation class for {@link EmailService}
 */
public class EmailServiceImpl  implements EmailService{

    private Log logger = LogFactory.getLog(EmailServiceImpl.class);

    private static String PROP_MAIL_HOST     = "mail.smtp.host";
    private static String PROP_MAIL_PROTOCOL = "mail.transport.protocol";
    private static String PROP_MAIL_FROM = "mail.from";

    private Session mailSession;


    @Override
    public void send(EmailPayloadInfo emailPayloadInfo) {

        JavaMailImpl mailer = new JavaMailImpl(this.getHost(), this
                .getProtocol(), true, null);
        if(null == emailPayloadInfo.getEmailFromAddress()){
            emailPayloadInfo.setEmailFromAddress(this.getFrom());
        }
        mailer.sendMail(emailPayloadInfo.getTitle(),
                        emailPayloadInfo.getGenericBodyContent(),
                        emailPayloadInfo.getEmailFromAddress(),
                        emailPayloadInfo.getRecipientAddresses(),
                        emailPayloadInfo.getCcAddresses(),
                        emailPayloadInfo.getBccAddresses(),
                        emailPayloadInfo.getHighPriority());

    }

    /**
     * @return Returns the host.
     */
    private String getHost() {
        return this.mailSession.getProperty(PROP_MAIL_HOST);
    }

    /**
     * @return Returns the from address.
     */
    private String getFrom() {
        return this.mailSession.getProperty(PROP_MAIL_FROM);
    }

    /**
     * @return Returns the protocol.
     */
    private String getProtocol() {
        return this.mailSession.getProperty(PROP_MAIL_PROTOCOL);
    }


    public void setMailSession(Session mailSession) {
        this.mailSession = mailSession;

        StringBuffer errMsg = new StringBuffer();
        boolean hostEmpty = false;
        boolean protocolEmpty = false;
        boolean fromEmpty = false;

        if (this.mailSession.getProperty(PROP_MAIL_HOST) == null || this.mailSession.getProperty(PROP_MAIL_HOST).length() == 0) {
            hostEmpty = true;
            errMsg.append(PROP_MAIL_HOST + "; ");
        }


        if (this.mailSession.getProperty(PROP_MAIL_PROTOCOL) == null || this.mailSession.getProperty(PROP_MAIL_PROTOCOL).length() == 0) {
            protocolEmpty = true;
            errMsg.append(PROP_MAIL_PROTOCOL + "; ");
        }

        if (this.mailSession.getProperty(PROP_MAIL_FROM) == null || this.mailSession.getProperty(PROP_MAIL_FROM).length() == 0) {
            fromEmpty = true;
            errMsg.append(PROP_MAIL_FROM + "; ");
        }

        if (hostEmpty || fromEmpty || protocolEmpty)
        {
            throw new RuntimeException("Mail properties are not defined: "
                    + errMsg.toString());
        }

        logger.debug(PROP_MAIL_HOST + "=" + this.mailSession.getProperty(PROP_MAIL_HOST));
        logger.debug(PROP_MAIL_PROTOCOL + "=" + this.mailSession.getProperty(PROP_MAIL_PROTOCOL));
        logger.debug(PROP_MAIL_FROM + "=" + this.mailSession.getProperty(PROP_MAIL_FROM));
    }
}
